#include "memory_demo_water.h"

void task_mem_info(void)
{
    printf("-----task memory water-----\n");
    LOS_MEM_POOL_STATUS poolStatus = {0};
    // pool为要统计信息的内存地址，此处以OS_SYS_MEM_ADDR为例
    void *pool = OS_SYS_MEM_ADDR;
    LOS_MemInfoGet(pool, &poolStatus);
    // 算出内存池当前的碎片率百分比
    float fragment = 100 - poolStatus.maxFreeNodeSize * 100.0 / poolStatus.totalFreeSize;
    // 算出内存池当前的使用率百分比
    float usage = poolStatus.totalUsedSize* 100.0 / LOS_MemPoolSizeGet(pool);
    printf("usage = %f, fragment = %f, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", \
            usage, fragment, poolStatus.maxFreeNodeSize, poolStatus.totalFreeSize, poolStatus.usageWaterLine);
}

UINT32 memory_entry_water(void)
{
    unsigned int ret, tid;
    TSK_INIT_PARAM_S param = {0};
    
    param.pfnTaskEntry = (TSK_ENTRY_FUNC)task_mem_info;
    param.uwStackSize  = 0x400*4;
    param.pcName       = "task_mem_info";
    param.usTaskPrio   = 10;
    // 锁住任务调度，防止高优先级任务调度
    LOS_TaskLock();
    // 创建任务
    ret = LOS_TaskCreate(&tid, &param);
    if (ret != LOS_OK) {
        printf("task memory water create failed, errno = 0x%x\n", ret);
    }
    // 解锁任务调度
    LOS_TaskUnlock();
    return ret;
}